<script setup>
import { onMounted, ref } from 'vue'
import { cityGetService, cityDelService } from '@/api/city'
import { Delete, Edit } from '@element-plus/icons-vue'
import CityEdit from './component/CityEdit.vue'
const CityList = ref([])
const getCityList = async () => {
  const res = await cityGetService()
  console.log(res.data.data)
  CityList.value = res.data.data
}
getCityList()
// const params = {
//   elementName: '',
//   elementImage: '',
//   tagId: ''
// }
const elementName = ref()

const isloading = ref(false)
const dialog = ref()
const dialogVisible = ref(false)

// 重置
const onReset = () => {
  elementName.value = ''
  getCityList()
}

// 搜索
const onSearch = async () => {
  try {
    if (!elementName.value) {
      console.warn('搜索的关键词不能为空')
      return
    }
    isloading.value = true

    const res = await cityGetService({ elementName: elementName.value })
    console.log('搜索结果', res.data.data)
    if (res && res.data && res.data.data) {
      CityList.value = res.data.data
    } else {
      CityList.value = [] // 如果没有结果，清空列表
    }
  } catch (error) {
    console.error('搜索失败:', error)
  } finally {
    isloading.value = false
  }
}
// 删除
const onDelete = async (row) => {
  let ids = row.id
  console.log('Delete article', ids)

  await ElMessageBox.confirm('是否要删除', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  if (!Array.isArray(ids)) {
    ids = [ids]
  }

  await cityDelService(ids)
  ElMessage.success('删除成功')
  getCityList()
  // 实现删除逻辑
}

//编辑
const onEdit = (row) => {
  console.log('编辑城市', row)
  dialog.value.open(row)
  // 实现编辑逻辑
}

//添加逻辑
const onAdd = () => {
  dialogVisible.value = true
  dialog.value.open({})
}
const onSuccess = () => {
  getCityList()
}

onMounted(getCityList)
</script>
<template>
  <page-container title="标签管理">
    <template #extra>
      <el-button type="primary" @click="onAdd">添加标签</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="标签名称">
        <el-input
          clearable
          placeholder="请输入标签名称"
          v-model="elementName"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="CityList" v-model="isloading">
      <el-table-column
        label="序号"
        width="200px"
        type="index"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="标签名称"
        prop="elementName"
        width="450px"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="类别"
        prop="tagId"
        width="450px"
        header-align="center"
        align="center"
      ></el-table-column>

      <el-table-column
        label="操作"
        width="530px"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEdit(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelete(row)"
          ></el-button> </template
      ></el-table-column>

      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <city-edit ref="dialog" @success="onSuccess"></city-edit>
  </page-container>
</template>
<style lang="scss" scoped></style>
